<template>
  <view class="environment-switcher">
    <view class="current-env" @click="showSwitcher = !showSwitcher">
      <text class="env-label">当前环境:</text>
      <text class="env-name">{{ currentEnvName }}</text>
      <text class="env-arrow">{{ showSwitcher ? '▲' : '▼' }}</text>
    </view>
    
    <view v-if="showSwitcher" class="env-options">
      <view 
        v-for="env in availableEnvironments" 
        :key="env.key"
        class="env-option"
        :class="{ active: env.key === currentEnv }"
        @click="switchToEnvironment(env.key)"
      >
        <text class="env-option-name">{{ env.name }}</text>
        <text class="env-option-url">{{ env.apiBaseUrl }}</text>
        <text v-if="env.key === currentEnv" class="env-check">✓</text>
      </view>
    </view>
  </view>
</template>

<script>
import { 
  getCurrentEnvironment, 
  getCurrentConfig, 
  getAvailableEnvironments, 
  switchEnvironment 
} from '@/config/environment.js';

export default {
  name: 'EnvironmentSwitcher',
  data() {
    return {
      showSwitcher: false,
      currentEnv: getCurrentEnvironment(),
      availableEnvironments: getAvailableEnvironments()
    };
  },
  computed: {
    currentEnvName() {
      const config = getCurrentConfig();
      return config.name;
    }
  },
  methods: {
    switchToEnvironment(envKey) {
      if (switchEnvironment(envKey)) {
        this.currentEnv = envKey;
        this.showSwitcher = false;
        
        // 显示切换成功提示
        uni.showToast({
          title: `已切换到${this.availableEnvironments.find(e => e.key === envKey).name}`,
          icon: 'success',
          duration: 2000
        });
      }
    }
  }
};
</script>

<style scoped>
.environment-switcher {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 9999;
  background: rgba(0, 0, 0, 0.8);
  border-radius: 8px;
  padding: 10px;
  min-width: 200px;
}

.current-env {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: white;
  padding: 8px 12px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.1);
  cursor: pointer;
}

.env-label {
  font-size: 12px;
  opacity: 0.8;
}

.env-name {
  font-size: 14px;
  font-weight: bold;
  margin: 0 8px;
}

.env-arrow {
  font-size: 10px;
  opacity: 0.6;
}

.env-options {
  margin-top: 8px;
  border-radius: 4px;
  overflow: hidden;
}

.env-option {
  display: flex;
  flex-direction: column;
  padding: 12px;
  background: rgba(255, 255, 255, 0.05);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  cursor: pointer;
  transition: background 0.2s;
  position: relative;
}

.env-option:hover {
  background: rgba(255, 255, 255, 0.1);
}

.env-option.active {
  background: rgba(76, 175, 80, 0.3);
}

.env-option-name {
  color: white;
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 4px;
}

.env-option-url {
  color: rgba(255, 255, 255, 0.6);
  font-size: 11px;
  word-break: break-all;
}

.env-check {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #4CAF50;
  font-weight: bold;
  font-size: 16px;
}
</style>
